import {combineReducers, configureStore} from "@reduxjs/toolkit"
import userReducer from "./user/userSlice"

import AsyncStorage from "@react-native-async-storage/async-storage"
import {persistStore, persistReducer} from "redux-persist"
import systemReducer from "./system/systemSlice"

// 持久化配置
const persistConfig = {
  key: "root", // 存储的键名
  storage: AsyncStorage, // 使用AsyncStorage作为存储引擎
  whitelist: ["user"], // 仅持久化指定Reducer
  // blacklist: ['otherReducer'], // 可选：排除指定Reducer
}

// 合并所有reducer
const rootReducer = combineReducers({
  user: userReducer,
  system: systemReducer,
})

// 创建持久化的reducer
const persistedReducer = persistReducer(persistConfig, rootReducer)

// 创建store
export const store = configureStore({
  reducer: persistedReducer,
  middleware: getDefaultMiddleware =>
    getDefaultMiddleware({serializableCheck: false}),
})

// 创建持久化存储实例
export const persistor = persistStore(store)
